// --- Copyright (c) 2006-2008 Stefan Kyntchev ---
// This software is written and copyrighted by Stefan Kyntchev 
// and BeyondPod Team members. All rights are reserved.
// Author contact: support@beyondpod.mobi
// ------------------------------------------------------------------------
// This file is part of BeyondPod RSS Feed Reader and Podcast manager
// (www.codeplex.com/BeyondPod) 
// BeyondPod is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// BeyondPod is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more details. 
//  
// You should have received a copy of the GNU General Public License
// along with BeyondPod. If not, see <http://www.gnu.org/licenses/>
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System.Drawing;
using System.IO;
using System.Text.RegularExpressions;
using svs.Mobile.BeyondMedia.Objects;

namespace svs.Mobile.BeyondMedia.Controls.Players.WindowsMediaPlayer
{
	public class WMPSkin : ISkin
	{
		private string m_Name;
		private string m_Path;
		private bool m_IsCurrent = false;
		private static Size m_CurrentSkinSize = new Size(0, 0);
		private Helpers.PlayerSkinOrientation m_Orientation = Helpers.PlayerSkinOrientation.Unknown;


		public WMPSkin(string i_Name, string i_Path, bool i_IsCurrent)
		{
			if (i_Name != null)
				m_Name = i_Name;
			else
				m_Name = Path.GetFileNameWithoutExtension(i_Path);
			
			m_Path = i_Path;
			m_IsCurrent = i_IsCurrent;
		}

		public WMPSkin(FileInfo i_skinFile, bool i_IsCurrent)
		{
			m_Name = Path.GetFileNameWithoutExtension(i_skinFile.FullName);
			m_Path = i_skinFile.FullName;
			m_IsCurrent = i_IsCurrent;
		}

		public string Name
		{
			get { return m_Name; }
			set { m_Name = value; }
		}

		public string SkinPath
		{
			get { return m_Path; }
			set { m_Path = value; }
		}

		public bool IsCurrent
		{
			get { return m_IsCurrent; }
			set { m_IsCurrent = value; }
		}

		/// <summary>
		/// Dimensions of the skin
		/// </summary>
		public Size SkinSize
		{
			get
			{
				if (m_CurrentSkinSize.Height == 0)
				{
					ReadSkin();
				}
				return m_CurrentSkinSize;
			}
		}

		public Helpers.PlayerSkinOrientation SkinOrientation
		{
			get
			{
				if (m_Orientation == Helpers.PlayerSkinOrientation.Unknown)
					ReadSkin();

				return m_Orientation;
			}
		}

		public override string ToString()
		{
			return string.Format("{0} ({1})", Name, SkinOrientation);
		}

		public override int GetHashCode()
		{
			return m_Path.GetHashCode();
		}

		public override bool Equals(object obj)
		{
			if (this == obj)
				return true;
			WMPSkin skin = obj as WMPSkin;
			if (skin == null)
				return false;
			if (!Equals(m_Path, skin.m_Path))
				return false;
			return true;
		}


		private void ReadSkin()
		{
			StreamReader reader = null;

			//Set defaults
			m_CurrentSkinSize.Width = 240;
			m_CurrentSkinSize.Height = 43;
			m_Orientation = Helpers.PlayerSkinOrientation.Portrait;


			bool gotSize = false, gotOrientation = false;

			try
			{
				if (File.Exists(SkinPath))
				{
					reader = File.OpenText(SkinPath);
					do
					{
						string skinSize = reader.ReadLine().Trim();
						Match m = Regex.Match(skinSize, @"Dimensions\s*(?<X>\d{1,3}),\s*(?<Y>\d{1,3})", RegexOptions.IgnoreCase);
						if (m.Success)
						{
							string width = m.Groups["X"].Value;
							string height = m.Groups["Y"].Value;

							m_CurrentSkinSize.Width = int.Parse(width);
							m_CurrentSkinSize.Height = int.Parse(height);
							gotSize = true;
						}

						Match mo = Regex.Match(skinSize, @"Orientation\s*(?<Orientation>Portrait|Landscape)", RegexOptions.IgnoreCase);
						if (mo.Success)
						{
							if (string.Compare(mo.Groups["Orientation"].Value, "Landscape", true) == 0)
								m_Orientation = Helpers.PlayerSkinOrientation.Landscape;
							else
								m_Orientation = Helpers.PlayerSkinOrientation.Portrait;

							gotOrientation = true;
						}
					} while (!reader.EndOfStream && (gotSize == false || gotOrientation == false));
				}
			}
			catch
			{
			}
			finally
			{
				if (reader != null)
					reader.Close();
			}
		}
	}
}